package com.lenovo.smarttraffic.ui.fragment;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.SensorData;
import com.lenovo.smarttraffic.bean.WeatherBean;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.widget.CircularProgressView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * @author Amoly
 * @date 2019/4/11.
 * description：主页面
 */
public class MainContentFragment extends BaseFragment {
    private static MainContentFragment instance = null;

    private Context context;
    private View view;


    @BindView(R.id.progress_circular)
    CircularProgressView circularProgressView;
    @BindView(R.id.value_tv)
    TextView value_tv;

    @BindView(R.id.temp_tv)
    TextView temp_tv;
    @BindView(R.id.weather_ic)
    View weather_ic;
    @BindView(R.id.weather_tv)
    TextView weather_tv;

    @BindView(R.id.temp_tv2)
    TextView temp_tv2;
    @BindView(R.id.weather_ic2)
    View weather_ic2;
    @BindView(R.id.weather_tv2)
    TextView weather_tv2;

    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv4)
    TextView tv4;

    private List<WeatherBean.ROWSDETAILBean> weather = new ArrayList<>();
    private SensorData sensorData;

    private Gson gson = new Gson();

    public static MainContentFragment getInstance() {
        if (instance == null) {
            instance = new MainContentFragment();
        }
        return instance;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    protected View getSuccessView() {
        view = View.inflate(getActivity(), R.layout.page_stage7, null);
        setListener();
        return view;
    }

    private void setListener() {
        HttpUtils.post("get_all_sense", new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> HttpUtils.post("get_weather", new HttpUtils().setParam("UserName", MainActivity.userName),
                        response2 -> {
                            sensorData = gson.fromJson(response.toString(), SensorData.class);
                            weather = gson.fromJson(response2.toString(), WeatherBean.class).getROWS_DETAIL();

                            temp_tv.setText("今日天气" + weather.get(0).getTemperature() + "℃");
                            temp_tv.setText("明日天气" + weather.get(1).getTemperature() + "℃");




                            //pm2.5
                            int pm25 = sensorData.getPM25();
                            Double i = pm25 / 500D * 100D;
                            circularProgressView.setProgress(i.intValue(), 1000);
                            value_tv.setText(pm25 > 200 ? "重度污染" : "轻度污染");
                            //
                            int lightIntensity = sensorData.getLightIntensity();
                            if (lightIntensity < 1000) {
                                tv1.setTextColor(0xff4472c4);
                                tv1.setText("弱");
                            } else if (lightIntensity > 1000 && lightIntensity < 3000) {
                                tv1.setTextColor(0xff00b050);
                                tv1.setText("中等");
                            } else {
                                tv1.setTextColor(0xffff0000);
                                tv1.setText("强");
                            }
                            //
                            int co2 = sensorData.getCo2();
                            if (co2 < 3000) {
                                tv2.setTextColor(0xff44dc68);
                                tv2.setText("适宜");
                            } else if (co2 > 3000 && co2 < 6000) {
                                tv2.setTextColor(0xffffc000);
                                tv2.setText("中等");
                            } else {
                                tv2.setTextColor(0xff8149ac);
                                tv2.setText("不宜");
                            }
                            //
                            int temperature = sensorData.getTemperature();
                            if (temperature < 12) {
                                tv3.setTextColor(0xff3462f4);
                                tv3.setText("寒冷");
                            }
                            if (temperature > 12 && temperature < 21) {
                                tv3.setTextColor(0xff92d050);
                                tv3.setText("舒适");
                            }
                            if (temperature > 21 && temperature < 35) {
                                tv3.setTextColor(0xff44dc68);
                                tv3.setText("温暖");
                            }
                            if (temperature > 35) {
                                tv3.setTextColor(0xffff0000);
                                tv3.setText("热");
                            }
                            //
                            int humidity = sensorData.getHumidity();
                            if (humidity < 50) {
                                tv4.setTextColor(0xffff0000);
                                tv4.setText("易发");
                            } else {
                                tv4.setTextColor(0xffffff40);
                                tv4.setText("少发");
                            }
                        }));
    }

    @Override
    protected Object requestData() {
        return Constant.STATE_SUCCEED;
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onDestroy() {
        if (instance != null) {
            instance = null;
        }
        super.onDestroy();
    }


}
